<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠
// +----------------------------------------------------------------------
namespace app\xqhz\controller;

use app\xqhz\model\Work as WorkModel;
use app\admin\controller\Admin;
/**
 * 事务控制器
 * 
 * @author 李忠
 * @createAt 2018-11-29 09:31:28

 *        
 */
class Work extends Admin {
	
	/**
	 * 列表页面
	 */
	public function index($title = null, $status = null) {
	 	cookie('__forward__', $this->request->url());

		// 数据列表
		$data_list = WorkModel::lists($title,$status);
		$this->assign('data_list', $data_list);
		$this->assign('pager', $data_list->render());

		$this->assign('data_status', config('data_status'));
		$this->assign('status', $status);
		$this->assign('title', $title);
		$this->setMeta ('事务管理');
		return $this->fetch ();
	}
	// 新增事务
	public function add() {
		if ($this->request->isPost()) {
			// 表单数据
			$data = $this->request->post();
			// 验证
			$result = $this->validate($data, 'Work');
			if (true !== $result) {
				$this->error($result);
			}
			if ($data = WorkModel::create($data)) {
				$this->log($data['id'], '新增事务成功');
				$this->success('新增事务成功', cookie('__forward__'));
			} else {
				$this->error('新增事务失败');
			}
		} else {
			$this->assign('info', null);
			$this->setMeta('新增事务');
			return $this->fetch();
		}
	}

	// 更新事务
	public function edit($id = null) {
		if ($this->request->isPost()) {
			// 表单数据
			$data = $this->request->post();
			// 验证
			$result = $this->validate($data, 'Work');
			if (true !== $result) {
				$this->error($result);
			}
			if ($data = WorkModel::update($data)) {

				$this->log($data['id'], '更新事务成功');
				$this->success('更新事务成功', cookie('__forward__'));
			} else {
				$this->error('更新事务失败');
			}

		} else {
			$info = WorkModel::get($id);

			$this->assign('info', $info);
			$this->setMeta('更新事务');
			return $this->fetch('add');
		}
	}

    /**
    * 禁用
    * @param int $id
    */
	public function forbid($id){
		if (WorkModel::state($id, -1)){
			$this->log($id, '禁用事务成功');
			$this->success('禁用事务成功');
		}else{
			$this->error('禁用事务失败');
		}
	}

    /**
    * 恢复
    * @param int $id
    */
	public function resume($id){
		if (WorkModel::state($id, -1)){
			$this->log($id, '启用事务成功');
		$this->success('启用事务成功');
		}else{
			$this->error('启用事务失败');
		}
	}

    /**
    * 删除
    * @param int $id
    */
	public function delete($id){
		if (WorkModel::state($id, -1)){
			$this->log($id, '删除事务成功');
		$this->success('删除事务成功');
		}else{
			$this->error('删除事务失败');
		}
	}

}